package handlers

import (
	"ticket/internal/db"
	"ticket/internal/db/model"
	"ticket/internal/schemas"
	"ticket/pkg/utils"

	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

type TeamHandler struct{}

func RegisterTeamRouter(group *gin.RouterGroup) {
	service := &TeamHandler{}
	group.POST("/edit", service.Create)
	group.GET("/:id", service.Get)
	group.GET("/list", service.List)
	group.DELETE("/delete/:id", service.Delete)
}

// Create godoc
// @Summary 编辑团队
// @Description 编辑团队
// @Tags 团队
// @ID /team/edit
// @Accept json
// @Produce json
// @Param team body schemas.CreateTeam true "团队信息"
// @Success 200 {object} schemas.Response{data=model.Team} "团队更新成功"
// @Failure 1004 {object} schemas.Response{error=string} "参数错误"
// @Router /team/edit [post]
func (service *TeamHandler) Create(ctx *gin.Context) {
	params := &schemas.CreateTeam{}
	if err := params.BindValidParam(ctx); err != nil {
		schemas.ResponseError(ctx, schemas.TeamParamInvalid, err)
		return
	}

	user, ok := utils.GetCurrentUser(ctx)
	if !ok {
		schemas.ResponseError(ctx, schemas.TeamParamInvalid, errors.New("用户ID不存在"))
		return
	}

	var members []model.User
	for _, id := range params.Members {
		members = append(members, model.User{ID: id})
	}
	team := &model.Team{
		ID:        params.ID,
		Name:      params.Name,
		Introduce: params.Introduce,
		Members:   members,
		CreatorBy: user.UserName,
	}
	if err := team.Create(db.GormDB); err != nil {
		schemas.ResponseError(ctx, schemas.TeamParamInvalid, err)
		return
	}
	schemas.ResponseSuccess(ctx, team)
}

// Get godoc
// @Summary 获取团队
// @Description 获取团队
// @Tags 团队
// @ID /team/:id
// @Accept json
// @Produce json
// @Param id path string true "团队ID"
// @Success 200 {object} schemas.Response{data=model.Team} "团队获取成功"
// @Router /team/:id [get]
func (service *TeamHandler) Get(ctx *gin.Context) {
	id := ctx.Param("id")
	team := &model.Team{}
	team, err := team.GetByID(db.GormDB, id)
	if err != nil {
		schemas.ResponseError(ctx, schemas.TeamParamInvalid, err)
		return
	}
	schemas.ResponseSuccess(ctx, team)
}

// List godoc
// @Summary 获取团队列表
// @Description 获取团队列表
// @Tags 团队
// @ID /team/list
// @Accept json
// @Produce json
// @Success 200 {object} schemas.Response{data=[]model.Team} "团队列表获取成功"
// @Router /team/list [get]
func (service *TeamHandler) List(ctx *gin.Context) {
	team := &model.Team{}
	teams, err := team.List(db.GormDB)
	if err != nil {
		schemas.ResponseError(ctx, schemas.TeamParamInvalid, err)
		return
	}
	schemas.ResponseSuccess(ctx, teams)
}

// Delete godoc
// @Summary 删除团队
// @Description 删除团队
// @Tags 团队
// @ID /team/delete/:id
// @Accept json
// @Produce json
// @Param id path int true "团队ID"
// @Success 200 {object} schemas.Response{data=string} "团队删除成功"
// @Failure 1004 {object} schemas.Response{error=string} "参数错误"
// @Router /team/delete/{id} [delete]
func (s *TeamHandler) Delete(ctx *gin.Context) {
	id := ctx.Param("id")
	if id == "" {
		schemas.ResponseError(ctx, schemas.ParamInvalid, errors.New("id不能为空"))
		return
	}
	team := &model.Team{}
	err := team.Delete(db.GormDB, id)
	if err != nil {
		schemas.ResponseError(ctx, schemas.TeamParamInvalid, err)
		return
	}
	schemas.ResponseSuccess(ctx, "团队删除成功")
}
